<?php

defined('_JEXEC') or die();

jimport('joomla.application.component.model');
require_once JPATH_COMPONENT.DS.'helpers'.DS.'extensionsHelper.php';

class Easiv2adminModelManageExtensions extends JModel {

	var $db;
	var $repoDir;
	var $extensionsHelper;
	var $parser;
	var $instances;
	var $packages;

	function __construct() {
		parent::__construct();

		$this->repoDir = 'repository';
		$this->extensionsHelper = new ExtensionsHelper($this->repoDir);
		$this->parser = new XmlParser();
		
		$this->db =& JFactory::getDBO();
		$this->db->setQuery("SELECT * FROM #__easiv2_instances ORDER BY id");
		$result = $this->db->loadObjectList();
		
		$this->instances = array();
		
		for($i=0; $i<count($result); $i++) {
			$this->instances[$i] = $result[$i];
		}

	}
	
	
	function getExtensionNames() {
		$this->db->setQuery("SELECT * FROM #__easiv2");
		$result = $this->db->loadObjectList();
		
		return $result;
	}
	
	function getLocalInstallStatus() {
		/*
		 * Array where install status is saved
		 */
		$installStatus = array();
		
		if(!JFolder::exists(JPATH_COMPONENT.DS.$this->repoDir)) {

			JFolder::create(JPATH_COMPONENT.DS.$this->repoDir);
		}

		$this->packages = JFolder::files(JPATH_COMPONENT.DS.$this->repoDir, ".zip");

		foreach($this->packages as $package) {
			$status = $this->extensionsHelper->unpack(JPATH_COMPONENT.DS.$this->repoDir.DS.$package);
			$extensionName = $this->extensionsHelper->getExtensionName();
			$installStatus[(string)$extensionName] = $status;
		}

		return $installStatus;
	}
	
	function getRemoteInstanceObjects() {
		return $this->instances;
	}

	function getRemoteInstallStatus() {

		$request_method = 'manageExtensions.isExtensionInstalled';
		
		/*
		 * Array in which the install information from remote instance will be saved
		 */
		$installStatus = array();
		$instancesInstallArray = array();
		
		if(count($this->instances > 0)) {
			
			for($i=0;$i < count($this->instances); $i++) {				
								
				$db =& JFactory::getDBO();				
				$db->setQuery('SELECT extension FROM #__easiv2');
				$result = $db->loadObjectList();
		
				foreach($result as $r) {
					$request_param = xmlrpc_encode($r->extension);
		
					$postdata = http_build_query(
						array(
					        'extension' => $request_param,
						)
					);
		
					$opts = array('http' =>	array(
					        'method'  => 'POST',
					        'header'  => 'Content-type: application/x-www-form-urlencoded',
							'content' => $postdata
						)
					);
		
					$context  = stream_context_create($opts);
					
					$instanceUrl = $this->instances[$i]->http_address;
					$response = file_get_contents($instanceUrl."/index.php?option=com_easiv2&task=".$request_method."&format=xmlrpc", false, $context);
					
//					echo "<br>REMOTE INSTALL STATUS FOR ".$r->extension." IS ".$response."<br>";
					$installStatus[$r->extension] = $response;
				}
				
				$instancesInstallArray[(string) $this->instances[$i]->name] = $installStatus;		
				
			}
			
			return $instancesInstallArray;
		}
		else {
			return null;
		}
	}

//	function getRemoteManagedExtensions() {
//
//		$request_method = 'manageExtensions.getInstalledExtensions';
//		$params = array();
//
//		$localExtensions = $this->getLocalRepositoryExtensions();
//
//		foreach($localExtensions as $e) {
//			$params[] = $e;
//		}
//
//		$request_params = xmlrpc_encode($params);
//
//		$postdata = http_build_query(
//		array(
//		        'parameters' => $request_params,
//		)
//		);
//
//		$opts = array('http' =>	array(
//	        'method'  => 'POST',
//	        'header'  => 'Content-type: application/x-www-form-urlencoded',
//			'content' => $postdata
//		)
//		);
//
//		$context  = stream_context_create($opts);
//		$response = file_get_contents("http://localhost/Joomla1.6RegularInstance/index.php?option=com_easiv2&task=".$request_method."&format=xmlrpc", false, $context);
//
//		return $response;
//	}
}